-- paquetage de base
with Ada.Text_IO, Ada.Integer_Text_IO;use Ada.Text_IO,Ada.Integer_Text_IO;

-- paquetages de gestion des exceptions
with Ada.Exceptions;use Ada.Exceptions;

-- paquetage du projet
with T_Fixe; use T_Fixe;

-- paquetages de l'interface Homme Machine (gtk)
with Gtk.Window, Gtk.Enums, Gtk.Button, Gtk.Container, Gtk.Table, Gtk.Text_View, Gtk.Text_Buffer, Gtk.Label, Gtk.Widget,Gtk.Handlers, Gtk.Marshallers, Gtk.Scrolled_Window, Gtk.Text_Mark, Gtk.Text_Iter;
use Gtk.Window, Gtk.Enums, Gtk.Button, Gtk.Container, Gtk.Table, Gtk.Text_View, Gtk.Text_Buffer, Gtk.Label, Gtk.Widget,Gtk.Handlers, Gtk.Marshallers, Gtk.Scrolled_Window, Gtk.Text_Mark, Gtk.Text_Iter;

with GLib, GLib.Convert; use GLib, GLib.Convert;


package body T_Telephone is

-------------------------------------------------------------
--                 IHM Configuration                       --
-------------------------------------------------------------


procedure Telephone_New(tel: ptr_Telephone;unNum : in GLib.Guint) is


	min : GLib.Gdouble := 1.0;
	max : GLib.Gdouble := 100.0;

begin

	tel.fixe := new T_Fixe.Fixe;
	tel.correspondant := new Telephone;
	tel.fixe.INIT(unNum);

	Gtk.Window.Gtk_New(tel.telephone);
	Gtk.Button.Gtk_New(tel.b_appeler,"Decrocher");
	Gtk.Button.Gtk_New(tel.b_parler,"Parler");

	Gtk.Spin_Button.Gtk_New(tel.ecran,min,max,min);

	Gtk.Text_View.Gtk_New(tel.msg_entrant);
	Set_Editable(tel.msg_entrant,false);Set_Cursor_Visible(tel.msg_entrant,false);Set_Sensitive(tel.msg_entrant,false);
	Gtk.Text_View.Gtk_New(tel.msg_sortant);

	Gtk.Table.Gtk_New(tel.t_fond,4,4,true);

	Gtk.Container.Add(Gtk.Container.Gtk_Container(tel.telephone),tel.t_fond);
	Gtk.Table.Attach(tel.t_fond, tel.ecran,0,3,3,4);
	Gtk.Table.Attach(tel.t_fond, tel.b_appeler, 3,4,3,4);
	Gtk.Table.Attach(tel.t_fond, tel.b_parler, 3,4,2,3);
	Gtk.Table.Attach(tel.t_fond, tel.msg_entrant, 0,3,0,2);
	Gtk.Table.Attach(tel.t_fond, tel.msg_sortant, 0,3,2,3);

	Gtk.Window.Show_All(tel.telephone);
	Gtk.Window.Set_Title(tel.telephone,"Telephone");

	Telephone_Callback.Connect
		(tel.b_appeler, "clicked",
		Telephone_Callback.To_Marshaller (Action_Appeler'Access),
		tel);
	Telephone_Callback.Connect
		(tel.b_parler, "clicked",
		Telephone_Callback.To_Marshaller (Action_Parler'Access),
		tel);
end Telephone_New;

procedure Action_Appeler (Button: access Gtk.Widget.Gtk_Widget_Record'Class; tel : ptr_Telephone)is
   begin

	Afficher(tel,"Appel en cours");
	tel.fixe.APPELER(Guint(Gtk.Spin_Button.Get_Value_As_Int(tel.ecran)));

	exception
	when E : others =>
		put_line("Reseau indisponible");
		put("Exception Identity : ");
		put_line(Exception_Name(Exception_Identity(E)));
		put("Exception Name : ");
		put_line(Exception_Name(E));
		put("Exception message : ");
		put_line(Exception_Message(E));
		put("Exception information : ");
		put_line(Exception_Information(E));
   end Action_Appeler;

procedure Action_Parler (Button: access Gtk.Widget.Gtk_Widget_Record'Class; tel : ptr_Telephone)is

Buffer : Gtk_Text_Buffer;
Start : Gtk_Text_Iter;
The_End : Gtk_Text_Iter;
   begin
	Buffer := Get_Buffer(tel.msg_sortant);
	Get_Bounds  (Buffer, Start, The_End);
	tel.fixe.PARLER(Locale_From_UTF8(Gtk.Text_Buffer.Get_Text(Buffer,Start,The_End)));
	Afficher(tel.correspondant,Locale_From_UTF8(Gtk.Text_Buffer.Get_Text(Buffer,Start,The_End)));

	exception
	when E : others =>
		put_line("Reseau indisponible");
		put("Exception Identity : ");
		put_line(Exception_Name(Exception_Identity(E)));
		put("Exception Name : ");
		put_line(Exception_Name(E));
		put("Exception message : ");
		put_line(Exception_Message(E));
		put("Exception information : ");
		put_line(Exception_Information(E));
   end Action_Parler;

procedure Mettre_en_relation(tel: ptr_Telephone; correspondant : ptr_Telephone) is
begin
	tel.correspondant := new Telephone;
	tel.correspondant := correspondant;
end Mettre_en_relation;


procedure Afficher(tel: ptr_Telephone; msg : in String) is
begin
	Set_Text(Get_Buffer(tel.msg_entrant),msg);
end Afficher;
-------------------------------------------------------------

end T_Telephone;